//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  onLoad: function () {
    var that = this;
    useUserInfo(this);
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        })
      }
    })
  },
  toCreat: function () {
    wx.navigateTo({
      url: '../../pages/creat/creat',
    })
  },
  toSingle: function () {
    wx.navigateTo({
      url: '../../pages/singleQuestion/singleQuestion',
    })
  }  ,
  onShareAppMessage: function () {
    // console.log(wx.getStorageSync("userCurrentPutQuestionId"));
    //console.log(wx.getStorageSync("userid"));
    return {
      title: '我要考考你',
      path: '/pages/index/index',
    }
  }
})


function useUserInfo(that) {
  if (wx.getStorageSync('userid')) {
    that.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  } else {
    //调用登录接口
    wx.login({
      success: function (rese) {
        wx.getUserInfo({
          success: function (res) {
            app.globalData.hasRoot = false;
            app.globalData.noRoot = true;
            wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
            app.globalData.userInfo = res.userInfo;
            var reg = /[A-Za - z0 - 9_\-\u4e00 - \u9fa5]+/
            if (!reg.test(res.userInfo.nickName)) {
              res.userInfo.nickName == "默认用户名"
            }
            that.setData({
              userInfo: res.userInfo
            })
            wx.request({
              url: app.globalData.allUrl + "/api/login.php",
              data: {
                code: rese.code,
              },
              header: { 'content-type': "application/x-www-form-urlencoded" },
              method: 'POST',
              success: function (datas) {
                wx.request({
                  url: app.globalData.allUrl + "/api/saveUserInfo.php",
                  data: {
                    user_id: datas.data.openid,
                    user_name: res.userInfo.nickName,
                    image_url: res.userInfo.avatarUrl,
                    //    referrerId: that.globalData.referrerId,
                  },
                  header: { 'content-type': "application/x-www-form-urlencoded" },
                  method: 'POST',
                  success: function (res) {
                    wx.setStorageSync('userid', datas.data.openid)
                  }, fail: function () {
                  }
                })
              }, fail: function () {
              }
            })
          }, fail: function () {
            wx.showModal({
              title: '提示',
              content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
              success: function (res) {
                if (res.confirm) {
                  wx.openSetting({
                    success: (res) => {
                      if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                        wx.getUserInfo({
                          success: function (res) {
                            app.globalData.hasRoot = false;
                            app.globalData.noRoot = true;
                            wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
                            app.globalData.userInfo = res.userInfo;
                            var reg = /[A-Za - z0 - 9_\-\u4e00 - \u9fa5]+/
                            if (!reg.test(res.userInfo.nickName)) {
                              res.userInfo.nickName == "默认用户名"
                            }
                            that.setData({
                              userInfo: res.userInfo
                            })
                            wx.request({
                              url: app.globalData.allUrl + "/api/login.php",
                              data: {
                                code: rese.code,
                              },
                              header: { 'content-type': "application/x-www-form-urlencoded" },
                              method: 'POST',
                              success: function (datas) {
                                wx.request({
                                  url: app.globalData.allUrl + "/api/saveUserInfo.php",
                                  data: {
                                    user_id: datas.data.openid,
                                    user_name: res.userInfo.nickName,
                                    image_url: res.userInfo.avatarUrl,
                                    //    referrerId: that.globalData.referrerId,
                                  },
                                  header: { 'content-type': "application/x-www-form-urlencoded" },
                                  method: 'POST',
                                  success: function (res) {
                                    wx.setStorageSync('userid', datas.data.openid)
                                  }, fail: function () {
                                  }
                                })
                              }, fail: function () {
                              }
                            })
                          }
                        })
                      }
                    }, fail: function (res) {

                    }
                  })

                }
              }
            })
          }
        })
      }
    })
  }

}